﻿using CommonEntity.CommonImageEntities;
using LiteDB;

namespace DbInfra;
public class ImageRepository : IImageRepository
{
    private readonly string _databasePath = @"ScienceAi.db";

    public void AddImageRecord(ImageRecord record)
    {
        using var db = new LiteDatabase(_databasePath);
        var collection = db.GetCollection<ImageRecord>("ImageRecordTable");
        collection.Insert(record);
    }

    public void UpdateImageRecord(ImageRecord record)
    {
        using var db = new LiteDatabase(_databasePath);
        var collection = db.GetCollection<ImageRecord>("ImageRecordTable");
        collection.Update(record);
    }

    public void DeleteImageRecord(int id)
    {
        using var db = new LiteDatabase(_databasePath);
        var collection = db.GetCollection<ImageRecord>("ImageRecordTable");
        collection.Delete(id);
    }

    public List<ImageRecord> QueryImageRecords(int pageNumber, int pageSize)
    {
        using var db = new LiteDatabase(_databasePath);
        var collection = db.GetCollection<ImageRecord>("ImageRecordTable");
        return collection.FindAll()
            .OrderByDescending(record => record.CreateTime)
            .Skip((pageNumber - 1) * pageSize)
            .Take(pageSize)
            .ToList();
    }

    public int GetTotalRecordCount()
    {
        using var db = new LiteDatabase(_databasePath);
        var collection = db.GetCollection<ImageRecord>("ImageRecordTable");
        return collection.Count();
    }

}
